<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['system:admin:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['system:admin:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['system:admin:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adminList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="微信昵称" align="center" prop="nickName" />
      <el-table-column label="手机号码" align="center" prop="phoneNum"></el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'info'">禁用</el-tag>
          <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="''">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:admin:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:admin:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改系统管理员微信绑定对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物业公司" prop="wxUserId">
          <el-select v-model="form.propertyId" placeholder="请选择微信昵称" size="mini"
                     style="width:100%;">
            <el-option label="请选择" value=""/>
            <el-option v-for="wxUser in propertyList"
                       :key="wxUser.id"
                       :label="wxUser.propertyName"
                       :value="wxUser.id"/>
          </el-select>
        </el-form-item>

        <el-form-item label="所属小区" prop="estateId">
          <el-select v-model="form.estateId" placeholder="请选择微信昵称" size="mini"
                     style="width:100%;" filterable clearable>
            <el-option label="请选择" value=""/>
            <el-option v-for="wxUser in estateList"
                       :key="wxUser.id"
                       :label="wxUser.estateName"
                       :value="wxUser.id"/>
          </el-select>
        </el-form-item>

        <el-form-item label="微信编号" prop="nickName">
          <el-input v-model="form.userNo" placeholder="请输入用户昵称" size="mini"
                    maxlength="30" @onblur="getListMpSearch">
            <template slot="append">
              <el-button @click="getListMpSearch" size="mini">搜索</el-button>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="微信昵称" prop="wxUserId">
          <el-select v-model="form.userId" placeholder="请选择微信昵称" size="mini"
                     style="width:100%;" @change="changeMpUser">
            <el-option label="请选择" value=""/>
            <el-option v-for="wxUser in wxUserOptions"
                       :key="wxUser.id"
                       :label="wxUser.nickName"
                       :value="wxUser.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state" size="mini">
            <el-radio :value="1" :label="1">正常</el-radio>
            <el-radio :value="0" :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="管理员" prop="isManager">
          <el-radio-group v-model="form.isManager" size="mini">
            <el-radio :value="1" :label="1">管理</el-radio>
            <el-radio :value="0" :label="0">非管理</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listAdmin, getAdmin, delAdmin, addAdmin, updateAdmin, exportAdmin } from "@/api/module/wx/admin";

  import { listDept} from "@/api/system/dept";
  import { listByNickName } from "@/api/module/wx/user";
  import {addPropertyUser, listUser, updateUser} from "@/api/module/property/user";
  import {listEstate} from "@/api/module/estate/estate";
  import {listProperty} from "@/api/module/property/property";
  export default {
    name: "Admin",
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        wxUserOptions:[],
        propertyList:[],
        estateList:[],
        deptOptions:[],
        queryParamsWxUser:{
          nickName:undefined,
          userNo:undefined,
        },
        // 系统管理员微信绑定表格数据
        adminList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          sysUserId: undefined,
        },
        queryParamsDept: {
          parentId: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      console.log('this.$route.query',this.$route.query)
      this.queryParams.sysUserId = this.$route.query.sysUserId ? this.$route.query.sysUserId : 0;
      this.form.sysUserId = this.queryParams.sysUserId;
      this.getList();
      this.getDeptList();
      this.getListEstate();
      this.getListProperty();
    },
    methods: {
      /** 查询系统管理员微信绑定列表 */
      getList() {
        this.loading = true;
        listUser(this.queryParams).then(response => {
          this.adminList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getDeptList() {
        this.queryParamsDept.parentId = 101;
        listDept(this.queryParamsDept).then(response => {
          this.deptOptions = response.data;
        });
      },
      changeMpUser(val){
        this.form.wxUserId = val;
      },
      getListMpSearch(){
        this.queryParamsWxUser.userNo = this.form.userNo;
        listByNickName(this.queryParamsWxUser).then(response => {
          this.wxUserOptions = response.data;
        });
      },
      getListEstate(){
        listEstate().then(response => {
          this.estateList = response.rows;
        });
      },
      getListProperty(){
        listProperty().then(response => {
          this.propertyList = response.rows;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          sysUserId: this.queryParams.sysUserId,
          name: undefined,
          wxUserId: undefined,
          nickName: undefined,
          deptId: undefined,
          deptName:undefined,
          state: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length!==1;
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加系统管理员微信绑定";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids;
        getUser(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改系统管理员微信绑定";
        });
      },

      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            console.log("form",this.form)
            if (this.form.id != null) {
              updateUser(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addPropertyUser(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除系统管理员微信绑定编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delAdmin(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有系统管理员微信绑定数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportAdmin(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
